package net.vinote.smart.platform.service.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

/**
 * 用户实体 缓存当前用户数据
 * 
 * @author 郑俊伟
 * 
 */
public class User {
	private String userID;
	private String userName;
	private String orgID;
	private String[] roleID;
	private Email email;
	private List<CodeLibrary> subjectList;

	private Map<String, List<AppInfo>> appListMap = new HashMap<String, List<AppInfo>>();

	private Map<String, List<AppItem>> appItemListMap = new HashMap<String, List<AppItem>>();

	/**
	 * 用户空间名
	 */
	private String spaceName;

	public User(UserInfo userInfo) {
		this.userID = userInfo.getUserID();
		this.userName = userInfo.getUserName();
		if (StringUtils.isNotBlank(userInfo.getConfig())) {
			JSONObject json = new JSONObject(userInfo.getConfig());
			if (json.has("email")) {
				setEmail(json.getJSONObject("email"));
			}
		}
	}

	/**
	 * @param json
	 */
	private void setEmail(JSONObject emailJson) {
		this.email = new Email();
		email.password = emailJson.getString("password");
		email.receiveProtocol = emailJson.getString("receive");
		email.receiveSSL = emailJson.getBoolean("receSSL");
		email.sendProtocol = emailJson.getString("send");
		email.sendSSL = emailJson.getBoolean("sendSSL");
		email.server = emailJson.getString("server");
		email.user = emailJson.getString("user");
	}

	public String[] getRoleID() {
		return roleID;
	}

	public void setRoleID(String[] roleID) {
		this.roleID = roleID;
	}

	public String getOrgID() {
		return orgID;
	}

	public void setOrgID(String orgID) {
		this.orgID = orgID;
	}

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public List<CodeLibrary> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<CodeLibrary> subjectList) {
		this.subjectList = subjectList;
	}

	public void setAppInfoList(String subject, List<AppInfo> appList) {
		appListMap.put(subject, appList);
	}

	public void setAppItemList(String appID, List<AppItem> itemList) {
		appItemListMap.put(appID, itemList);
	}

	public List<AppInfo> getAppInfoBySubject(String subjectNo) {
		return appListMap.get(subjectNo);
	}

	public List<AppItem> getAppItemByAppID(String appID) {
		return appItemListMap.get(appID);
	}

	public Email getEmail() {
		return email;
	}

	public String getSpaceName() {
		if (spaceName == null) {
			spaceName = userID;
		}
		return spaceName;
	}

	public class Email {
		private String server;
		private String user;
		private String password;
		private String sendProtocol;
		private boolean sendSSL;
		private String receiveProtocol;
		private boolean receiveSSL;

		public String getServer() {
			return server;
		}

		public String getUser() {
			return user;
		}

		public String getPassword() {
			return password;
		}

		public String getSendProtocol() {
			return sendProtocol;
		}

		public String getReceiveProtocol() {
			return receiveProtocol;
		}

		public boolean isSendSSL() {
			return sendSSL;
		}

		public boolean isReceiveSSL() {
			return receiveSSL;
		}

	}
}
